﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WindowsGameLibrary1;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace Bipoland
{
    public class Belmont : Entity
    {
        private AnimationPlayer animationPlayer;
        private Vector2 position;
        private int directionX;
        private int directionY;
        private int orientation;
        private int speedx;
        private int width;

        public int DirectionX
        {
            get { return directionX; }
            set { directionX = value; }
        }

        public int DirectionY
        {
            get { return directionY; }
            set { directionY = value; }
        }

        public int Orientation
        {
            get { return orientation; }
            set { orientation = value; }
        }


        public Belmont(int width)
        {
            animationPlayer = new AnimationPlayer();
            position = new Vector2(0, 240);
            directionX = 0;
            directionY = 0;
            orientation = 1;
            speedx = 15;
            this.width = width;

        }

        public Vector2 Position1
        {
            get { return position; }
            set { position = value; }
        }

        #region Entity Members
        public void LoadContent(ContentManager Content)
        {
            // Belmont Parado
            LoadStandUpBelmont(Content);

            // Belmont Agachado
            LoadCrouchBelmont(Content);

            // Belmont Caminando
            LoadWalkingAnimation(Content);

            // Belmont Atacando

            LoadAttackBelmont(Content);


        }

        private void LoadAttackBelmont(ContentManager Content)
        {
            Frame atacando1 = new Frame();
            atacando1.Duration = 0.1F;
            atacando1.Sprite = Content.Load<Texture2D>("BelmontAtacandoParado/BelmontPegandoParado1");
            atacando1.Origin = new Vector2(17f, 25f);
            atacando1.FlipOrigin = new Vector2(16f, 25f);


            CollisionObject belmont1 = new CollisionObject();
            CollisionCoordinates cc1 = new CollisionCoordinates(new Vector2(-9, -18), new Vector2(13, 24), ObjectCollisionType.Weak);
            List<CollisionCoordinates> lcc1 = new List<CollisionCoordinates>();

            lcc1.Add(new CollisionCoordinates(new Vector2(-4, -12), new Vector2(7, 24), ObjectCollisionType.Weak));

            belmont1.Rectangle = cc1;
            belmont1.Rectangles = lcc1;
            atacando1.CollisionObject = belmont1;

            CollisionObject belmont1flip = new CollisionObject();
            CollisionCoordinates cc1flip = new CollisionCoordinates(new Vector2(-9, -18), new Vector2(13, 24), ObjectCollisionType.Weak);
            List<CollisionCoordinates> lcc1flip = new List<CollisionCoordinates>();

            lcc1flip.Add(new CollisionCoordinates(new Vector2(-7, -11), new Vector2(3, 24), ObjectCollisionType.Weak));

            belmont1flip.Rectangle = cc1flip;
            belmont1flip.Rectangles = lcc1flip;

            atacando1.FlipCollisionObject = belmont1flip;


            Frame atacando2 = new Frame();
            atacando2.Duration = 0.1F;
            atacando2.Sprite = Content.Load<Texture2D>("BelmontAtacandoParado/BelmontPegandoParado2");
            atacando2.Origin = new Vector2(24f, 25f);
            atacando2.FlipOrigin = new Vector2(15f, 25f);



            CollisionObject belmont2 = new CollisionObject();
            CollisionCoordinates cc2 = new CollisionCoordinates(new Vector2(-4, -17), new Vector2(13, 24), ObjectCollisionType.Weak);
            List<CollisionCoordinates> lcc2 = new List<CollisionCoordinates>();

            lcc2.Add(new CollisionCoordinates(new Vector2(-3, -13), new Vector2(5, 24), ObjectCollisionType.Weak));

            belmont2.Rectangle = cc2;
            belmont2.Rectangles = lcc2;
            atacando2.CollisionObject = belmont2;

            CollisionObject belmont2flip = new CollisionObject();
            CollisionCoordinates cc2flip = new CollisionCoordinates(new Vector2(-12, -16), new Vector2(10, 24), ObjectCollisionType.Weak);
            List<CollisionCoordinates> lcc2flip = new List<CollisionCoordinates>();

            lcc2flip.Add(new CollisionCoordinates(new Vector2(-6, -10), new Vector2(3, 24), ObjectCollisionType.Weak));

            belmont2flip.Rectangle = cc2flip;
            belmont2flip.Rectangles = lcc2flip;

            atacando1.FlipCollisionObject = belmont1flip;


            Frame atacando3 = new Frame();
            atacando3.Duration = 0.1F;
            atacando3.Sprite = Content.Load<Texture2D>("BelmontAtacandoParado/BelmontPegandoParado3");
            atacando3.Origin = new Vector2(34f, 24f);
            atacando3.FlipOrigin = new Vector2(16f, 24f);


            CollisionObject belmont3 = new CollisionObject();
            CollisionCoordinates cc3 = new CollisionCoordinates(new Vector2(-17, -15), new Vector2(14, 25), ObjectCollisionType.Weak);
            List<CollisionCoordinates> lcc3 = new List<CollisionCoordinates>();

            lcc3.Add(new CollisionCoordinates(new Vector2(-6, -10), new Vector2(4, 25), ObjectCollisionType.Weak));

            belmont3.Rectangle = cc3;
            belmont3.Rectangles = lcc3;
            atacando3.CollisionObject = belmont3;

            CollisionObject belmont3flip = new CollisionObject();
            CollisionCoordinates cc3flip = new CollisionCoordinates(new Vector2(-10, -15), new Vector2(14, 25), ObjectCollisionType.Weak);
            List<CollisionCoordinates> lcc3flip = new List<CollisionCoordinates>();

            lcc3flip.Add(new CollisionCoordinates(new Vector2(-3, -10), new Vector2(5, 24), ObjectCollisionType.Weak));

            belmont3flip.Rectangle = cc3flip;
            belmont3flip.Rectangles = lcc3flip;

            atacando3.FlipCollisionObject = belmont3flip;


            Frame atacando4 = new Frame();
            atacando4.Duration = 0.1F;
            atacando4.Sprite = Content.Load<Texture2D>("BelmontAtacandoParado/BelmontPegandoParado4");
            atacando4.Origin = new Vector2(36f, 25f);
            atacando4.FlipOrigin = new Vector2(16f, 25f);


            CollisionObject belmont4 = new CollisionObject();
            CollisionCoordinates cc4 = new CollisionCoordinates(new Vector2(-15, -18), new Vector2(31, 25), ObjectCollisionType.Weak);
            List<CollisionCoordinates> lcc4 = new List<CollisionCoordinates>();

            lcc4.Add(new CollisionCoordinates(new Vector2(-4, -11), new Vector2(4, 24), ObjectCollisionType.Weak));

            belmont4.Rectangle = cc4;
            belmont4.Rectangles = lcc4;
            atacando4.CollisionObject = belmont4;

            CollisionObject belmont4flip = new CollisionObject();
            CollisionCoordinates cc4flip = new CollisionCoordinates(new Vector2(-14, -17), new Vector2(15, 24), ObjectCollisionType.Weak);
            List<CollisionCoordinates> lcc4flip = new List<CollisionCoordinates>();

            lcc4flip.Add(new CollisionCoordinates(new Vector2(-6, -12), new Vector2(5, 25), ObjectCollisionType.Weak));

            belmont4flip.Rectangle = cc4flip;
            belmont4flip.Rectangles = lcc4flip;

            atacando4.FlipCollisionObject = belmont4flip;




            Frame atacando5 = new Frame();
            atacando5.Duration = 0.1F;
            atacando5.Sprite = Content.Load<Texture2D>("BelmontAtacandoParado/BelmontPegandoParado5");
            atacando5.Origin = new Vector2(36f, 25f);
            atacando5.FlipOrigin = new Vector2(16f, 24f);



            CollisionObject belmont5 = new CollisionObject();
            CollisionCoordinates cc5 = new CollisionCoordinates(new Vector2(-13, -15), new Vector2(10, 23), ObjectCollisionType.Weak);
            List<CollisionCoordinates> lcc5 = new List<CollisionCoordinates>();

            lcc5.Add(new CollisionCoordinates(new Vector2(-3, -11), new Vector2(3, 23), ObjectCollisionType.Weak));

            belmont5.Rectangle = cc5;
            belmont5.Rectangles = lcc5;
            atacando1.CollisionObject = belmont1;

            CollisionObject belmont5flip = new CollisionObject();
            CollisionCoordinates cc5flip = new CollisionCoordinates(new Vector2(-15, -14), new Vector2(-11, 24), ObjectCollisionType.Weak);
            List<CollisionCoordinates> lcc5flip = new List<CollisionCoordinates>();

            lcc5flip.Add(new CollisionCoordinates(new Vector2(0, -9), new Vector2(-5, 25), ObjectCollisionType.Weak));

            belmont5flip.Rectangle = cc5flip;
            belmont5flip.Rectangles = lcc5flip;

            atacando5.FlipCollisionObject = belmont5flip;




            Frame atacando6 = new Frame();
            atacando6.Duration = 0.1F;
            atacando6.Sprite = Content.Load<Texture2D>("BelmontAtacandoParado/BelmontPegandoParado6");
            atacando6.Origin = new Vector2(27f, 26f);
            atacando6.FlipOrigin = new Vector2(23f, 26f);



            CollisionObject belmont6 = new CollisionObject();
            CollisionCoordinates cc6 = new CollisionCoordinates(new Vector2(-15, -12), new Vector2(14, 22), ObjectCollisionType.Weak);
            List<CollisionCoordinates> lcc6 = new List<CollisionCoordinates>();

            lcc6.Add(new CollisionCoordinates(new Vector2(-18, -10), new Vector2(-1, 23), ObjectCollisionType.Weak));

            belmont6.Rectangle = cc6;
            belmont6.Rectangles = lcc6;
            atacando6.CollisionObject = belmont6;

            CollisionObject belmont6flip = new CollisionObject();
            CollisionCoordinates cc6flip = new CollisionCoordinates(new Vector2(-11, -11), new Vector2(11, 23), ObjectCollisionType.Weak);
            List<CollisionCoordinates> lcc6flip = new List<CollisionCoordinates>();

            lcc6flip.Add(new CollisionCoordinates(new Vector2(-9, -10), new Vector2(1, 23), ObjectCollisionType.Weak));

            belmont6flip.Rectangle = cc6flip;
            belmont6flip.Rectangles = lcc6flip;

            atacando6.FlipCollisionObject = belmont6flip;



            Frame atacando7 = new Frame();
            atacando7.Duration = 0.1F;
            atacando7.Sprite = Content.Load<Texture2D>("BelmontAtacandoParado/BelmontPegandoParado7");
            atacando7.Origin = new Vector2(32f, 26f);
            atacando7.FlipOrigin = new Vector2(51f, 26f);




            CollisionObject belmont7 = new CollisionObject();
            CollisionCoordinates cc7 = new CollisionCoordinates(new Vector2(-16, -14), new Vector2(48, 23), ObjectCollisionType.Weak);
            List<CollisionCoordinates> lcc7 = new List<CollisionCoordinates>();

            lcc7.Add(new CollisionCoordinates(new Vector2(-5, -9), new Vector2(0, 23), ObjectCollisionType.Weak));
            lcc7.Add(new CollisionCoordinates(new Vector2(16, -8), new Vector2(49, -5), ObjectCollisionType.Strong));

            belmont7.Rectangle = cc7;
            belmont7.Rectangles = lcc7;
            atacando7.CollisionObject = belmont7;

            CollisionObject belmont7flip = new CollisionObject();
            CollisionCoordinates cc7flip = new CollisionCoordinates(new Vector2(-50, -16), new Vector2(16, 23), ObjectCollisionType.Weak);
            List<CollisionCoordinates> lcc7flip = new List<CollisionCoordinates>();

            lcc7flip.Add(new CollisionCoordinates(new Vector2(-8, -10), new Vector2(6, 23), ObjectCollisionType.Weak));
            lcc7flip.Add(new CollisionCoordinates(new Vector2(-65, -8), new Vector2(-18, -5), ObjectCollisionType.Strong));

            belmont7flip.Rectangle = cc7flip;
            belmont7flip.Rectangles = lcc7flip;

            atacando7.FlipCollisionObject = belmont7flip;




            Frame atacando8 = new Frame();
            atacando8.Duration = 0.1F;
            atacando8.Sprite = Content.Load<Texture2D>("BelmontAtacandoParado/BelmontPegandoParado8");
            atacando8.Origin = new Vector2(20f, 26f);
            atacando8.FlipOrigin = new Vector2(65f, 26f);



            CollisionObject belmont8 = new CollisionObject();
            CollisionCoordinates cc8 = new CollisionCoordinates(new Vector2(-14, -15), new Vector2(65, 23), ObjectCollisionType.Weak);
            List<CollisionCoordinates> lcc8 = new List<CollisionCoordinates>();

            lcc8.Add(new CollisionCoordinates(new Vector2(-6, -10), new Vector2(1, 23), ObjectCollisionType.Weak));
            lcc8.Add(new CollisionCoordinates(new Vector2(17, -7), new Vector2(65, -4), ObjectCollisionType.Strong));

            belmont8.Rectangle = cc8;
            belmont8.Rectangles = lcc8;
            atacando8.CollisionObject = belmont8;

            CollisionObject belmont8flip = new CollisionObject();
            CollisionCoordinates cc8flip = new CollisionCoordinates(new Vector2(-65, -13), new Vector2(16, 23), ObjectCollisionType.Weak);
            List<CollisionCoordinates> lcc8flip = new List<CollisionCoordinates>();

            lcc8flip.Add(new CollisionCoordinates(new Vector2(-4, -11), new Vector2(7, 23), ObjectCollisionType.Weak));
            lcc8flip.Add(new CollisionCoordinates(new Vector2(-65, -8), new Vector2(-17, -4), ObjectCollisionType.Strong));

            belmont8flip.Rectangle = cc8flip;
            belmont8flip.Rectangles = lcc8flip;

            atacando8.FlipCollisionObject = belmont8flip;






            Animation atacando = new Animation();
            atacando.AnimationTime = 0.8f;
            atacando.SpriteCount = 8;

            atacando.AddFrame(atacando1);
            atacando.AddFrame(atacando2);
            atacando.AddFrame(atacando3);
            atacando.AddFrame(atacando4);
            atacando.AddFrame(atacando5);
            atacando.AddFrame(atacando6);
            atacando.AddFrame(atacando7);
            atacando.AddFrame(atacando8);

            animationPlayer.AddAnimation(AnimationPlayerEnum.ATACANDO, atacando);
        }

        private void LoadStandUpBelmont(ContentManager Content)
        {
            Frame parado1 = new Frame();
            parado1.Duration = 0.3f;
            parado1.Sprite = Content.Load<Texture2D>("BelmontParado/BelmontParado1");
            parado1.Origin = new Vector2(12f, 27f);
            parado1.FlipOrigin = new Vector2(19f, 27f);

            Frame parado2 = new Frame();
            parado2.Duration = 0.3f;
            parado2.Sprite = Content.Load<Texture2D>("BelmontParado/BelmontParado2");
            parado2.Origin = new Vector2(14f, 27f);
            parado2.FlipOrigin = new Vector2(17f, 27f);

            Frame parado3 = new Frame();
            parado3.Duration = 0.3f;
            parado3.Sprite = Content.Load<Texture2D>("BelmontParado/BelmontParado3");
            parado3.Origin = new Vector2(14f, 27f);
            parado3.FlipOrigin = new Vector2(17f, 27f);

            Animation parado = new Animation();
            parado.SpriteCount = 3;
            parado.AnimationTime = 0.9f;
            parado.AddFrame(parado1);
            parado.AddFrame(parado2);
            parado.AddFrame(parado3);
            animationPlayer.AddAnimation(AnimationPlayerEnum.PARADO, parado);
            animationPlayer.ChangeAnimation(AnimationPlayerEnum.PARADO, ModeEnum.Ever);
            animationPlayer.Play();
        }

        private void LoadCrouchBelmont(ContentManager Content)
        {
            Frame agachado1 = new Frame();
            agachado1.Duration = 0.1f;
            agachado1.Sprite = Content.Load<Texture2D>("BelmontAgachado/BelmontAgachado1");
            agachado1.Origin = new Vector2(15f, 28f);
            agachado1.FlipOrigin = new Vector2(15f, 28f);

            Frame agachado2 = new Frame();
            agachado2.Duration = 0.1f;
            agachado2.Sprite = Content.Load<Texture2D>("BelmontAgachado/BelmontAgachado2");
            agachado2.Origin = new Vector2(16f, 28f);
            agachado2.FlipOrigin = new Vector2(16f, 28f);

            Frame agachado3 = new Frame();
            agachado3.Duration = 0.1f;
            agachado3.Sprite = Content.Load<Texture2D>("BelmontAgachado/BelmontAgachado3");
            agachado3.Origin = new Vector2(16f, 28f);
            agachado3.FlipOrigin = new Vector2(16f, 28f);

            Animation agachado = new Animation();
            agachado.SpriteCount = 3;
            agachado.AnimationTime = 0.3f;
            agachado.AddFrame(agachado1);
            agachado.AddFrame(agachado2);
            agachado.AddFrame(agachado3);

            animationPlayer.AddAnimation(AnimationPlayerEnum.AGACHADO, agachado);
        }

        private void LoadWalkingAnimation(ContentManager Content)
        {

            Frame caminando1 = new Frame();
            caminando1.Duration = 0.1f;
            caminando1.Sprite = Content.Load<Texture2D>("BelmontCaminando/BelmontCaminando1");
            caminando1.Origin = new Vector2(16f, 25f);
            caminando1.FlipOrigin = new Vector2(11f, 25f);

            Frame caminando2 = new Frame();
            caminando2.Duration = 0.1f;
            caminando2.Sprite = Content.Load<Texture2D>("BelmontCaminando/BelmontCaminando2");
            caminando2.Origin = new Vector2(15f, 25f);
            caminando2.FlipOrigin = new Vector2(15f, 25f);

            Frame caminando3 = new Frame();
            caminando3.Duration = 0.1f;
            caminando3.Sprite = Content.Load<Texture2D>("BelmontCaminando/BelmontCaminando3");
            caminando3.Origin = new Vector2(15f, 25f);
            caminando3.FlipOrigin = new Vector2(16f, 25f);


            Frame caminando4 = new Frame();
            caminando4.Duration = 0.1f;
            caminando4.Sprite = Content.Load<Texture2D>("BelmontCaminando/BelmontCaminando4");
            caminando4.Origin = new Vector2(17f, 25f);
            caminando4.FlipOrigin = new Vector2(18f, 25f);

            Frame caminando5 = new Frame();
            caminando5.Duration = 0.1f;
            caminando5.Sprite = Content.Load<Texture2D>("BelmontCaminando/BelmontCaminando5");
            caminando5.Origin = new Vector2(16f, 24f);
            caminando5.FlipOrigin = new Vector2(17f, 24f);

            Frame caminando6 = new Frame();
            caminando6.Duration = 0.1f;
            caminando6.Sprite = Content.Load<Texture2D>("BelmontCaminando/BelmontCaminando6");
            caminando6.Origin = new Vector2(16f, 25f);
            caminando6.FlipOrigin = new Vector2(14f, 25f);

            Frame caminando7 = new Frame();
            caminando7.Duration = 0.1f;
            caminando7.Sprite = Content.Load<Texture2D>("BelmontCaminando/BelmontCaminando7");
            caminando7.Origin = new Vector2(15f, 25f);
            caminando7.FlipOrigin = new Vector2(14f, 25f);

            Frame caminando8 = new Frame();
            caminando8.Duration = 0.1f;
            caminando8.Sprite = Content.Load<Texture2D>("BelmontCaminando/BelmontCaminando8");
            caminando8.Origin = new Vector2(19f, 25f);
            caminando8.FlipOrigin = new Vector2(17f, 25f);

            Animation caminando = new Animation();
            caminando.SpriteCount = 8;
            caminando.AnimationTime = 0.8f;
            caminando.AddFrame(caminando1);
            caminando.AddFrame(caminando2);
            caminando.AddFrame(caminando3);
            caminando.AddFrame(caminando4);
            caminando.AddFrame(caminando5);
            caminando.AddFrame(caminando6);
            caminando.AddFrame(caminando7);
            caminando.AddFrame(caminando8);
            animationPlayer.AddAnimation(AnimationPlayerEnum.CAMINANDO, caminando);
        }

        public void Update(GameTime gameTime)
        {
            animationPlayer.update(gameTime);
            position.X += directionX * speedx;
            Scrolling.getInstance().updatePos(new Vector2 (position.X-(width/2),position.Y));

        }

        public void Draw(SpriteBatch spriteBatch, GameTime gameTime, float order, float scroller)
        {
            OrientBelmontMove();


            animationPlayer.Draw(spriteBatch, gameTime, new Vector2(position.X-scroller,position.Y), order);
        }

        private void OrientBelmontMove()
        {
            if (orientation > 0)
            {
                animationPlayer.SpriteEffects = SpriteEffects.None;
            }
            else
            {
                if (orientation < 0)
                {
                    animationPlayer.SpriteEffects = SpriteEffects.FlipHorizontally;
                }

            }
        }
        public void crouchAnimation()
        {
            animationPlayer.ChangeAnimation(AnimationPlayerEnum.AGACHADO, ModeEnum.One);
            animationPlayer.Play();
        }

        public void StandUpAnimation()
        {
            animationPlayer.ChangeAnimation(AnimationPlayerEnum.PARADO, ModeEnum.Ever);
            animationPlayer.Play();
        }

        public void WalkingAnimation()
        {
            animationPlayer.ChangeAnimation(AnimationPlayerEnum.CAMINANDO, ModeEnum.Ever);
            animationPlayer.Play();
        }

        public void attackingAnimation()
        {
            animationPlayer.ChangeAnimation(AnimationPlayerEnum.ATACANDO, ModeEnum.One);
            animationPlayer.Play();
        }

        public Vector2 Position()
        {
            return position;
        }

        public float TotalAnimationTime()
        {
            return animationPlayer.GiveAnimationTime();
        }

        public Vector2 ImageSize()
        {
            throw new NotImplementedException();
        }

        public void CollisionAction(Entity entity)
        {
            throw new NotImplementedException();
        }

        public BoundingBox CollisionRectangle()
        {
            Vector3 min = new Vector3(position.X + animationPlayer.GiveCollisionRectangle().Minimus.X, position.Y + animationPlayer.GiveCollisionRectangle().Minimus.Y, 0);

            Vector3 max = new Vector3(position.X + animationPlayer.GiveCollisionRectangle().Maximus.X, position.Y + animationPlayer.GiveCollisionRectangle().Maximus.Y, 0);

            return new BoundingBox(min, max);
        }

        public List<CollisionCoordinates> CollisionRectangles()
        {
            return animationPlayer.GiveCollisionRectangles();
        }

        public float GivePaintOrder()
        {
            return 0;
        }

        public bool isdying()
        {
            return false;
        }

        #endregion

    }

}
